<?php
include_once 'data.php';
include_once 'functions.php';

database_connect($database_path, 'library');

if (isset($_GET['delete']) && isset($_GET['file']) && isset($dbHandle)) {
    $error = delete_record($dbHandle, $_GET['file']);
    die ();
}

if (isset($_GET['file'])) {

    $id_query = $dbHandle->quote($_GET['file']);
    $result = $dbHandle->query("SELECT * FROM library WHERE id=$id_query LIMIT 1");
    $paper = $result->fetch(PDO::FETCH_ASSOC);
    $result = null;

    $current_record = array_search($_GET['file'], $_SESSION['export_files']);
    isset($_SESSION['export_files'][$current_record-1]) ? $prevrecord = $_SESSION['export_files'][$current_record-1] : $prevrecord = null;
    isset($_SESSION['export_files'][$current_record+1]) ? $nextrecord = $_SESSION['export_files'][$current_record+1] : $nextrecord = null;

    //Monica- Modification to restrict delete function to owner or admin
    $userSession=$dbHandle->quote($_SESSION['user']);
    database_connect($usersdatabase_path, 'users');
    $users = $dbHandle->query("SELECT userID,username,permissions FROM users WHERE username = $userSession");
    $userActual=$users->fetch(PDO::FETCH_ASSOC);
    $dbHandle = null;
    
    $deleteFile=false;
    if($userActual['userID']==$paper['added_by'] || $userActual['permissions']=='A' ){
    	$deleteFile=true;
    }
	//Ends  modification
}

$dbHandle = null;
?>
<div class="display-content" id="file-item-<?php echo $paper['id']?>" style="margin:0px;padding:0px">
    <table cellspacing="0" class="top" style="margin-top:2px;margin-bottom:1px">
        <tr>
            <td class="top" style="width: 55%">
                <div class="ui-state-highlight ui-corner-top" id="file-item" style="float:left;margin-left:2px">
                    <span class="ui-icon ui-icon-home" style="float:left;width:16px"></span>Item&nbsp;
                </div>
                <?php
                if (isset($_SESSION['auth'])) {

                    if ($_SESSION['permissions'] != 'G') {
                        ?>
                <div class="ui-state-highlight ui-corner-top" id="file-edit" style="float:left;margin-left:2px">
                    <span class="ui-icon ui-icon-gear" style="float:left"></span>Edit&nbsp;
                </div>
                        <?php
                    }
                    ?>
                <div class="ui-state-highlight ui-corner-top" id="file-notes" style="float:left;margin-left:2px">
                    <span class="ui-icon ui-icon-pencil" style="float:left"></span>Notes&nbsp;
                </div>
                <div class="ui-state-highlight ui-corner-top" id="file-categories" style="float:left;margin-left:2px">
                    <span class="ui-icon ui-icon-tag" style="float:left"></span>Categories&nbsp;
                </div>
                <div class="ui-state-highlight ui-corner-top" id="file-files" style="float:left;margin-left:2px">
                    <span class="ui-icon ui-icon-document" style="float:left"></span>Files&nbsp;
                </div>
                <div class="ui-state-highlight ui-corner-top" id="file-discussion" style="float:left;margin-left:2px">
                    <span class="ui-icon ui-icon-comment" style="float:left"></span>Discuss&nbsp;
                </div>
                <?php
                }
                ?>
            </td>
            <td class="top" style="width:45%">
                <div class="ui-state-highlight backbutton ui-corner-top" style="float:right;margin-left:2px;margin-right:4px" title="Back to list view">
                    <span class="ui-icon ui-icon-squaresmall-close" style="float:left"></span>Close&nbsp;
                </div>
                <?php
                if (isset($_SESSION['auth'])) {

                    if ($_SESSION['permissions'] != 'G') {
						if($deleteFile){
                        ?>
                <form action="file.php" method="GET" id="deleteform">
                    <input type="hidden" name="file" value="<?php print htmlspecialchars($_GET['file']) ?>">
                    <input type="hidden" name="select" value="<?php print htmlspecialchars($_GET['select']) ?>">
                    <input type="hidden" name="delete" value="delete">
                    <div class="ui-state-highlight ui-corner-top" style="float:right;margin-left:2px" id="deletebutton">
                        <span class="ui-icon ui-icon-trash" style="float:left"></span>Delete&nbsp;
                    </div>
                </form>
                        <?php
                    }
				}
                    ?>
                <div class="ui-state-highlight ui-corner-top" style="float:right;margin-left:2px" id="printbutton">
                    <span class="ui-icon ui-icon-print" style="float:left"></span>Print&nbsp;
                </div>
                <div id="exportfilebutton" class="ui-state-highlight ui-corner-top" style="float:right;margin-left:2px">
                    <span class="ui-icon ui-icon-suitcase" style="float:left"></span>Export&nbsp;
                </div>
                <div class="ui-state-highlight ui-corner-top" style="float:right;margin-left:2px">
                    <a href="mailto:?subject=Paper in I, Librarian&body=<?php print htmlspecialchars(wordwrap($paper['title_ascii'], "75","%0A", false))
                       .'%0A%0A'.htmlspecialchars(wordwrap(substr($paper['abstract_ascii'],0,512), "75","%0A", false))
                       .htmlspecialchars(empty($paper['doi']) ? '' : '%0A%0APublisher link:%0Ahttp://dx.doi.org/'.$paper['doi'])
                       .'%0A%0AI, Librarian link:%0A'.htmlspecialchars($url.'stable.php?id='.$paper['id'])
                       .(file_exists('library/'.$paper['file']) ? '%0A%0ADirect link to the PDF:%0A'.htmlspecialchars($url.'library/'.$paper['file']) : '') ?>"
                       target="_blank" style="color:black;display:inline-block">
                        <span class="ui-icon ui-icon-mail-closed" style="float:left"></span>E-Mail&nbsp;</a>
                </div>
                    <?php
                }
                ?>
            </td>
        </tr>
    </table>
    <?php
    if (!empty($paper['id'])) {

        print '<div id="file-panel" style="width:100%;height:48%;border-top:1px solid #c6c8cc;border-bottom:1px solid #c6c8cc;overflow:auto">';
        include 'file_top.php';
        print '</div>';

        print '<table class="top" cellspacing=0 style="width:100%"><tr><td style="width:20em">';

        print '<div class="ui-state-highlight" id="leftindex-handle1" style="float:left;margin-right:4px" title="Resize left panel">
		<span class="ui-icon ui-icon-triangle-2-e-w" ></span></div>';

        if (isset($_SESSION['auth']) && is_file("library/$paper[file]")) {
            print '<div id="reload" class="ui-state-highlight" style="float:left">
		     <span class="ui-icon ui-icon-refresh" style="float:left"></span>Reload&nbsp;
		    </div>';
        }

        print '&nbsp;</td><td style="text-align:center">';

        print '<table cellspacing=0 style="margin:auto"><tr><td>';

        print '<div class="ui-state-highlight'.(empty($prevrecord) ? ' ui-state-disabled' : ' prevrecord').'" id="prev-item-'.$prevrecord.'">
		   <span class="ui-icon ui-icon-triangle-1-n" style="float:left"></span>Prev&nbsp;
		  </div>';

        print '</td><td style="padding:0px 8px">';

        print '<div class="ui-state-highlight backbutton noprint" title="Back to list view">
		  <span class="ui-icon ui-icon-squaresmall-close" style="float:left"></span>Close&nbsp;
		</div>';

        print '</td><td>';

        print '<div class="nextrecord ui-state-highlight'.(empty($nextrecord) ? ' ui-state-disabled' : '').'" id="next-item-'.$nextrecord.'">
		  <span class="ui-icon ui-icon-triangle-1-s" style="float:left"></span>Next&nbsp;
              </div>';

        print '</td></tr></table>';

        print '</td><td style="width:20em">';

        if (isset($_SESSION['auth']) && is_file("library/$paper[file]")) {
            print '<div class="ui-state-highlight" id="file-pgdown" style="width:7em;float:right;margin-left:4px">Scroll Down</div>';
            print '<div class="ui-state-highlight noprint file-pgup" style="width:6em;float:right">Scroll Up</div>';
        }

        print '<div id="resizeframe" class="ui-state-highlight" style="float:right;padding-left:2px;margin-right:4px" title="Resize top panel"><span class="ui-icon ui-icon-triangle-2-n-s"></span></div>';

        print '&nbsp;</td></tr></table>';

        if (is_file("library/$paper[file]") && isset($_SESSION['auth'])) {

            if (!isset($_SESSION['pdfviewer']) || (isset($_SESSION['pdfviewer']) && $_SESSION['pdfviewer'] == 'external'))
		print PHP_EOL."<table id=\"pdf-table\" cellspacing=0 style=\"width:100%;height:100%;border-top:1px solid #c6c8cc;border-bottom:1px solid #c6c8cc\"><tr><td>
			<iframe name=\"pdf\" class=\"pdf-file\" src=\"".htmlspecialchars("library/$paper[file]#pagemode=none&scrollbar=1&navpanes=0&toolbar=1&statusbar=0&page=1&view=FitH,0")."\"
				frameborder=\"0\" class=\"noprint\" style=\"width:100%;height:100%\"></iframe></td></tr></table>";

            if (isset($_SESSION['pdfviewer']) && $_SESSION['pdfviewer'] == 'internal')
            print PHP_EOL.'<table id="pdf-table" class="noprint" cellspacing=0 style="width:100%;height:100%;border-top:1px solid #c6c8cc;border-bottom:1px solid #c6c8cc"><tr><td>
			<iframe name="pdf" class="pdf-file" src="'.htmlspecialchars('viewpdf.php?file='.$paper['file']).'&navpanes=0"
				frameborder="0" style="width:100%;height:100%"></iframe></td></tr></table>';

            print '<div class="ui-state-highlight noprint file-pgup" style="width:6em;float:right">Scroll Up</div>';

            print '<div class="ui-state-highlight" id="leftindex-handle2" style="float:left" title="Resize left panel">
		<span class="ui-icon ui-icon-triangle-2-e-w" ></span></div>';

            print '<div class="ui-state-highlight backbutton noprint" style="width:6em;margin-left:47%" title="Back to list view">
			   <span class="ui-icon ui-icon-squaresmall-close" style="float:left"></span>Close&nbsp;
		    </div>';

        } elseif (isset($_SESSION['auth'])) {

            print '<div style="margin-top:10%;margin-left:45%;color:#b6b8bc;font-size:28px;width:200px"><b>No PDF</b></div>';
        }

    } else {
        print '<p><b>Record does not exist.</b>';
    }

    include_once 'coins.php';
    ?>
</div>
<script type="text/javascript">
<?php if (!empty($error)) print "$('#dialog-error').html('<p><span class=\"ui-state-error-text\"><span class=\"ui-icon ui-icon-alert\" style=\"float:left;margin:0px 7px 20px 0px\"></span></span>$error</p>').dialog('option','title','Error!').dialog('open');"; ?>
</script>